// axios进行分装
import axios from 'axios'
import router from '../router'
import Vue from 'vue'

axios.defaults.timeout = 9999999
// axios.defaults.baseURL = 'http://localhost:8001/'
axios.defaults.baseURL = 'http://localhost:8003/' // 反向代理
// 这句代码很关键配置跨域
axios.defaults.withCredentials = true

// http request 拦截器
axios.interceptors.request.use(
  config => {
    let token = localStorage.token // 注意使用的时候需要引入cookie方法，推荐js-cookie
    if (config.method === 'post') { // 如果是post请求 请求体自动加上token
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      config.data = config.data
      if (config.url.endsWith('login')) {
        return config
      }
      if (token) {
        // 头部加上token
        config.headers.token = token
      } else if (token == null) {
        Vue.prototype.$message.error('token失效')
        router.push({
          path: '/login'
        })
      }
    } else if (config.method === 'get') { // 如果是get请求 url中拼接上
      if (token) {
        // 头部加上token
        config.headers.token = token
      } else if (token == null) {
        Vue.prototype.$message.error('token失效')
        router.push({
          path: '/login'
        })
        console.log('token失效')
      }
      // 头部加上token
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    }
    return config
  }, error => {
    // return Promise.reject(err)
    console.log(error)
  }
)

// http response 拦截器
axios.interceptors.response.use(
  // token 可以判断已过期，重定向到登录页面
  response => {
    if (response.data.code === 501) {
      Vue.prototype.$message.error('没有登录请重新登录')
      router.push({
        path: '/login',
        querry: {
          redirect: router.currentRoute.fullPath
        } // 从哪个页面跳转
      })
    }
    return response
  }, error => {
    const {
      status
    } = error.response
    if (status === 501) {
      Vue.prototype.$message.error('没有登录请重新登录')
      router.push({
        path: '/login'
      })
    }
    if (status === 500) {
      Vue.prototype.$message.warning(response.data.data)
    }
    return Promise.reject(error)
  }
)

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get (url, params = {}) {
  return new Promise((resolve, reject) => {
    axios({
      url: url,
      method: 'GET',
      params: params,
      transformRequest: [function (data) {
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }]
    })
      .then(res => {
        if (res.data.code === 1001) {
          this.$Message.warning('登录超时,' + res.data.message)
          this.$router.push({
            path: '/'
          })
          return
        }
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios({
      url: url,
      method: 'POST',
      data: data,
      transformRequest: [function (data) {
        // console.log(data)
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }]
    })
      .then(res => {
        if (res.data.code === 1001) {
          this.$Message.warning('用户超时' + res.data.message)
          this.$router.push({
            path: '/'
          })
          return
        }
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * from 表单
 */
export function get_from (URL, PARAMS) {
  var p = new Promise((resolve, reject) => { // 做一些异步操作
    var temp = document.createElement('form')
    temp.action = URL
    temp.method = 'get'
    temp.style.display = 'none'
    for (var x in PARAMS) {
      var opt = document.createElement('textarea')
      opt.name = x
      opt.value = PARAMS[x]
      temp.appendChild(opt)
    }
    document.body.appendChild(temp)
    temp.submit()
    this.$Loading.finish()
    resolve(temp)
  })
  return p
}

/**
 * 上传 excel
 */

export function up_excel (URL, PARAMS) {
  return new Promise((resolve, reject) => {
    let token = this.$Cookies.get('token')
    let config = {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
    PARAMS.append('token', token)

    axios.post(URL, PARAMS, config)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}
